﻿import D3D.Vertex;
import D3D.Matrix;
import D3D.Polygon;
import D3D.Style;
class D3D.Screen {
	//---------------------------
	public var matrix:Matrix;
	public var style:Style;
	public var polygons:Object;
	private var tag:MovieClip;
	private var me:Object;
	//----------------------------- 构造函数
	public function Screen(screenMc:MovieClip) {
		if (screenMc == undefined || eval(screenMc) == undefined) {
			say("场景创建目标不正确 "+screenMc);
			return;
		} else {
			tag = screenMc;
		}
		polygons = new Array();
		matrix = new Matrix();
		me = this;
	}
	//----------------------------- 功能函数
	public function Rotation(x,y,z,p)
	{
		if(p==undefined){
			for(var i in polygons)
			{
				//polygons[i].center=matrix.zRotation(polygons[i].center,x,y,z);
				polygons[i].RotationCenter(x,y,z);
			}
		}else if(p.pname!=undefined){
			//p.center=matrix.zRotation(p.center,x,y,z);
			p.RotationCenter(x,y,z);
			
		}else if(p[0]==undefined)
		{
			for(var i in p)
			{
				if(me.polygons[p[i]]!=undefined)
				{
					//me.polygons[p[i]].center=matrix.zRotation(me.polygons[p[i]].center,x,y,z);
					me.polygons[p[i]].RotationCenter(x,y,z);
					
				}
			}
		}
	}
	public function Move(x,y,z,p)
	{
		if(p==undefined){
			for(var i in polygons)
			{
				//polygons[i].center=matrix.zMove(polygons[i].center,x,y,z);
				polygons[i].MoveCenter(x,y,z);
			}
		}else if(p.pname!=undefined){
			//p.center=matrix.zMove(p.center,x,y,z);
			p.MoveCenter(x,y,z);
		}else if(p[0]==undefined)
		{
			for(var i in p)
			{
				if(me.polygons[p[i]]!=undefined)
				{
					//me.polygons[p[i]].center=matrix.zMove(me.polygons[p[i]].center,x,y,z);
					me.polygons[p[i]].MoveCenter(x,y,z);
				}
			}
		}
	}
	public function addPolygon(polys) {
		if (polys.length>0) {
			for (var i in polys) {
				me.polygons[polys[i].pname] = polys[i];
				polys.screen = me;
			}
		} else if (polys.pname != undefined) {
			for (var i in me.polygons) {
				if (polys.pname == i) {
					say("多边形名重复! "+polys.pname);
					return false;
				}
			}
			me.polygons[polys.pname] = polys;
			polys.screen = me;
		}
		return true;
	}
	public function Draw() {
		tag.jsq = 0;
		tag.objList = new Array();
		for (var i in polygons) {
			var ptag:MovieClip;
			if (tag[polygons[i].pname] == undefined) {
				tag.createEmptyMovieClip(polygons[i].pname, tag.jsq);
				ptag = tag[polygons[i].pname];
				tag.jsq++;
			} else {
				clears(tag[polygons[i].pname]);
				ptag = tag[polygons[i].pname];
			}
			polygons[i].Draw(ptag);
			setDepth(ptag, polygons[i]);
		}
	}
	
	public function toString(){
		trace("----  Screen  ----");
		trace("style:");
		me.style.toString();
		trace("");
		trace("matrix:");
		me.matrix.toString();
		trace("");
		trace("polygon:");
		for(var i in polygons)
		{
			trace(i+":");
			polygons[i].toString();
		}
		trace("------------------");
	}
	private function setDepth(tag:MovieClip, poly:Polygon):Boolean {
		var a:Array = tag._parent.objList;
		if (a.length>0) {
			var i = 0;
			var b = new Array();
			while (i<a.length) {
				if (a[i].z>poly.z) {
					break;
				} else {
					b.push(a[i]);
				}
				i++;
			}
			b[i].tag = tag;
			b[i].z = poly.z;
			a[i].tag.swapDepths(tag);
			for (var l = i; l<a.length; i++) {
				b.push(a[l]);
			}
		} else {
			a.push({tag:tag, z:poly.z});
		}
		return true;
	}
	private function clears(tag) {
		for (var i in tag) {
			if (typeof (tag[i]) == "movieclip") {
				me.clears(tag[i]);
			}
		}
	}
	private function say(o:Object) {
		trace("Screen:"+o);
	}
}
